package com.thesharegame.dal;

import java.util.List;

import com.thesharegame.dal.dao.DAOFactory;
import com.thesharegame.dal.dao.DataAccessObject;
import com.thesharegame.dal.util.ObjectUtils;

public class DALService {

	public DALService(){
		
	}
	
	public boolean saveObjects(List<? extends Object> objects){
		for(Object object : objects){
			this.saveObject(object);
		}
		return true;
	}
	
	public boolean saveObject(Object object){
		DAOFactory factory = DAOFactory.getInstance();
		DataAccessObject dao = factory.getDAO(object);
		if(ObjectUtils.isPersistedObject(object)){
			return dao.update(object);
		}else{
			return dao.save(object);
		}
	}
	
	public List<Object> fetch(Object prototype){
		DAOFactory factory = DAOFactory.getInstance();
		DataAccessObject dao = factory.getDAO(prototype);
		List<Object> results = dao.find(prototype);
		return results;
	}
	
	public boolean deleteObject(Object object){
		DAOFactory factory = DAOFactory.getInstance();
		DataAccessObject dao = factory.getDAO(object);
		return dao.delete(object);
	}
}
